package com.example.importtest;

import com.example.configuration.BeanConfiguration;
import com.example.configuration.Teacher;
import com.example.importtest.register.BeanRegistrar;
import com.example.importtest.register.EnableBeanRegistrar;
import com.example.importtest.selector.BeanConf;
import com.example.importtest.selector.BeanImportSelector;
import com.example.importtest.selector.EnableBeanSelector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Optional;
/*
@EnableAutoConfiguration 自动装配
在 edu-springboot-configuration 中有两个 Enable 注解，一个 spring.factories
在 edu-springboot-autoconfigure 中测试 Enable 注解和 spring.factories，实现 bean 的自动装配

@EnableBeanSelector：通过 ImportSelector 接口实现 bean 注入 IOC
@EnableBeanRegistrar：通过 ImportBeanDefinitionRegistrar 接口手动实现 bean 注入 IOC
spring.factories：配置了带有 @Configuration 的配置类，如果没有配置，在 import 中无法自动装配

以上三种方式，均可以实现 bean 的自动装配

测试过程：
测试的 bean 是 Teacher 这个类，三种装配配置了不同的 name
测试时，需要保证 @EnableBeanSelector、@EnableBeanRegistrar、spring.factories 只有一个生效，因此要按需注释注解、spring.factories内容
 */

@SpringBootApplication
//@EnableBeanSelector
//@EnableBeanRegistrar("苍老师")
public class EduSpringbootAutoconfigureApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(EduSpringbootAutoconfigureApplication.class, args);
        showEnableSelector(run);
        showEnableRegistrar(run);
        showConfiguration(run);
    }

    public static void showConfiguration(ApplicationContext run) {
        try {
            run.getBean(BeanConfiguration.class);
            final Teacher bean1 = run.getBean(Teacher.class);
            System.out.println(bean1);
        } catch (Exception e) {
            System.out.println("spring.factories 没有 BeanConfiguration 自动配置");
        }
    }

    public static void showEnableSelector(ApplicationContext run) {

        EnableBeanSelector selector = EduSpringbootAutoconfigureApplication.class.getAnnotation(EnableBeanSelector.class);

        if (!Optional.ofNullable(selector).isPresent()) {
            return;
        }

        final BeanConf bean1 = run.getBean(BeanConf.class);
        System.out.println(bean1);

        final Teacher bean2 = run.getBean(Teacher.class);
        System.out.println(bean2);

        try {
            final BeanImportSelector bean3 = run.getBean(BeanImportSelector.class);
            System.out.println(bean3);
        } catch (Exception e) {
            System.out.println("ImportSelector 不会注入 IOC");
        }
    }

    public static void showEnableRegistrar(ApplicationContext run) {

        EnableBeanRegistrar registrar = EduSpringbootAutoconfigureApplication.class.getAnnotation(EnableBeanRegistrar.class);

        if (!Optional.ofNullable(registrar).isPresent()) {
            return;
        }

        final Teacher bean1 = run.getBean(Teacher.class);
        System.out.println(bean1);

        try {
            final BeanRegistrar bean2 = run.getBean(BeanRegistrar.class);
            System.out.println(bean2);
        } catch (Exception e) {
            System.out.println("ImportBeanDefinitionRegistrar 不会注入 IOC");
        }
    }

}
